﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading;
using Hydra.Contract;
using Hydra.Payload.Logging;
using Trace = Hydra.Payload.Logging.Trace;

namespace Hydra.Payload.Hooking
{
    public class SimpleHookProc : ICustomHookProc
    {
        private Process proc = Process.GetCurrentProcess();

        public virtual void PreCall(string function, params HyraHookParam[] objs)
        {
            WriteLog("PreCall", function, objs);
        }

        public virtual void PostCall(string function, params HyraHookParam[] objs)
        {
            WriteLog("PostCall", function, objs);
        }

        public virtual void GetReturnValue(string function, HyraHookParam obj)
        {
            WriteLog("ReturnValue", function, obj);
        }


        public virtual bool HookPreCall { get { return true; } }
        public virtual bool HookPostCall { get { return false; } }
        public virtual bool HookReturnValue { get { return true; } }

        protected virtual void WriteLog(string step, string function, params HyraHookParam[] objs)
        {
            Trace.Info("[{0}][{1}({2})][{3}] {4} {5}", DateTime.Now.ToString("HH:mm:ss.f"), proc.ProcessName, proc.Id, Thread.CurrentThread.ManagedThreadId, step, function);
            foreach (var par in objs)
            {
                Trace.Info("\t[{0} | {1}]: {2}", par.Name, par.Type.FullName, par.Value);
            }
        }
    }
}
